<template>
	<view class="search-box">
		<view class="uni-searchbar">
			<view :style="{borderRadius:radius+'px',backgroundColor: bgColor}" class="uni-searchbar__box">
				<view class="search" v-if="hascity">
					<text>{{city}}</text>
					<uni-icons color="#000" size="18" type="arrowdown" />
				</view>
				<view class="split" v-if="hascity">
					<text>|</text>
				</view>
				<!-- #ifdef MP-ALIPAY -->
				<view class="uni-searchbar__box-icon-search" @click="confirm">
					<uni-icons color="#999999" size="18" type="search" />
				</view>
				<!-- #endif -->
				
				<!-- #ifndef MP-ALIPAY -->
				<uni-icons color="#999999" class="uni-searchbar__box-icon-search" size="18" type="search" @click="confirm" />
				<!-- #endif -->
				
				<input v-if="show" 
				:focus="showSync" 
				:placeholder="placeholder" 
				:maxlength="maxlength" 
				class="uni-searchbar__box-search-input" 
				@confirm="confirm" 
				confirm-type="search" 
				type="text"
				:value="searchVal"
				@input="handleInput"
				 />
				<text v-else class="uni-searchbar__text-placeholder">{{ placeholder }}</text>
				<view v-if="show && (clearButton==='always'||clearButton==='auto'&&searchVal!=='')" class="uni-searchbar__box-icon-clear" @click="clear">
					<uni-icons color="#999999" class="" size="24" type="clear" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniIcons from "../uni-icons/uni-icons.vue";
	export default {
		components:{
			uniIcons
		},
		props: {
			hascity:{
				type:Boolean,
				default:true
			},
			city:{
				type: String,
				default: "济南"
			},
			placeholder: {
				type: String,
				default: "城市名/景点名"
			},
			radius: {
				type: [Number, String],
				default: 25
			},
			clearButton: {
				type: String,
				default: "auto"
			},
			cancelButton: {
				type: String,
				default: "auto"
			},
			cancelText: {
				type: String,
				default: '取消'
			},
			bgColor: {
				type: String,
				default: "#F8F8F8"
			},
			maxlength: {
				type: [Number, String],
				default: 100
			},
			show:{
				type: Boolean,
				default: false
			},
			value: {
				type: String,
				default: ""
			},
		},
		data() {
			return {
				showSync: true,
				searchVal: ""
			}
		},
		watch: {
			value:{
				immediate:true,
				handler(val){
					this.searchVal = val;
				}
			}
		},
		onLoad() {
			// uni.getLocation({
			//     type: 'wgs84',
			//     success: function (res) {
			//         console.log('当前位置的经度：' + res.longitude);
			//         console.log('当前位置的纬度：' + res.latitude);
			//     }
			// });
		},
		methods: {
			handleInput(event){
				const value = event.target.value;
				this.searchVal = value;
				this.$emit('input', value);
			},
			clear(){
				this.searchVal = ""
				this.$emit('input', "");
			},
			confirm() {
				// #ifndef APP-PLUS
				uni.hideKeyboard();
				// #endif
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord()
				// #endif
				this.$emit("confirm", {
					value: this.searchVal
				})
			}
		}
	}
</script>

<style>
	.search-box{
		width: 100%;
		background-color: #fff;
	}
	.uni-searchbar {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 85%;
		margin: 0 auto;
		flex-direction: row;
		position: relative;
		padding: 16rpx;
		background-color: #ffffff;
	}
	
	.uni-searchbar__box {
		/* #ifndef APP-NVUE */
		display: flex;
		box-sizing: border-box;
		/* #endif */
		overflow: hidden;
		position: relative;
		flex: 1;
		justify-content: left;
		flex-direction: row;
		align-items: center;
		height: 36px;
		padding: 10rpx 16rpx 10rpx 40rpx;
		border-width: 0.5px;
		border-style: solid;
		border-color: #e5e5e5;
	}
	.search{
		line-height: 32px;
	}
	.split{
		color: #c9c9c9;
		padding: 5rpx 20rpx;
	}
	.uni-searchbar__box-icon-search {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		width: 32px;
		justify-content: center;
		align-items: center;
		color: #808080;
	}
	
	.uni-searchbar__box-search-input {
		flex: 1;
		font-size: 28rpx;
		color: #333;
	}
	
	.uni-searchbar__box-icon-clear {
		align-items: center;
		line-height: 24px;
		padding-left: 5px;
	}
	
	.uni-searchbar__text-placeholder {
		font-size: 28rpx;
		color: #808080;
		margin-left: 5px;
	}
	
	.uni-searchbar__cancel {
		padding-left: 10px;
		line-height: 36px;
		font-size: 14px;
		color: #333;
	}
</style>
